package cn.ymatou.admin.falcon.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.utils.ObjSerialize;
import com.ymttest.utils.YMTDateUtil;

import cn.ymatou.admin.falcon.parameter.GetReportBean;
import cn.ymatou.admin.falcon.parameter.ReportData;
import cn.ymatou.admin.falcon.parameter.echarts.GroupChart;
import cn.ymatou.admin.falcon.parameter.echarts.Serie;
import cn.ymatou.admin.falcon.service.FindAllCall;
import cn.ymatou.admin.falcon.service.GetDataCall;
import cn.ymatou.admin.falcon.service.GetReportCall;

/**
 * 日志分析 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetReport {
	private static GetReportBean getreportBean;
	private static GetReportCall getreportCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("日志分析");
	}

	@Before
	public void caseUp() {
		getreportBean = new GetReportBean();
		getreportCall = new GetReportCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getreportCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private List<String> getYmtDomain() throws ClientProtocolException, IOException {
		FindAllCall FindAllCall = new FindAllCall();
		FindAllCall.callService();
		List<String> ymtdomain = FindAllCall.getResult().stream().filter(res -> {
			return res.contains("ymatou");
		}).collect(Collectors.toList());
		return ymtdomain;
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetReport_001() {
		Logger.start(true, "日志分析,抓去domain的report序列化本地持久化");
		try {
			//{domain={[time={[url={avg=,pv=,999=}}]]}}
			TreeMap<String, Map> allDomainReport = new TreeMap<String, Map>();
			this.getYmtDomain().forEach(domain -> {
				String start = "2018-11-15 23:55";
				Integer offset = 60;
				Integer times = 16;//
				Date sdate = YMTDateUtil.getDate(start, "yyyy-MM-dd HH:mm");
				Date edate = YMTDateUtil.getBeforeOrNextSecond(sdate, offset);
				Map<String, ArrayList<ReportData>> listReportDatas = new HashMap<String, ArrayList<ReportData>>();
				for (int i = 0; i < times; i++) {
					getreportCall = new GetReportCall();
					getreportBean.setAppId(domain);
					getreportBean.setStartTime(YMTDateUtil.getFormatDateString(sdate, "yyyy-MM-dd HH:mm"));
					getreportBean.setEndTime(YMTDateUtil.getFormatDateString(edate, "yyyy-MM-dd HH:mm"));
					getreportCall.setData(getreportBean);
					try {
						getreportCall.callService();
						listReportDatas.put(YMTDateUtil.getFormatDateString(sdate, "ddHHmm"), getreportCall.getResult());
					} catch (Exception e) {
						e.printStackTrace();
					}
					sdate = edate;
					edate = YMTDateUtil.getBeforeOrNextSecond(sdate, offset);
				}
				TreeMap<String, Map<String, Map>> getReport = new TreeMap<String, Map<String, Map>>();

				listReportDatas.entrySet().stream().forEach(rd -> {
					Map tm = new HashMap<>();
					rd.getValue().forEach(rpdinfo -> {
						Map<String, Object> data = new HashMap<>();
						tm.put("url", rpdinfo.getMethod());
						data.put("pv", rpdinfo.getTotal() == null ? 0 : rpdinfo.getTotal());
						data.put("avg", rpdinfo.getAvg() == null ? 0 : rpdinfo.getAvg());
						data.put("999", rpdinfo.getNinetyNineDotNineLine());
						tm.put(rpdinfo.getMethod(), data);
					});
					getReport.put(rd.getKey(), tm);
				});
				allDomainReport.put(getreportBean.getAppId(), getReport);
			});
			ObjSerialize.Serialize(allDomainReport, "allDomainReport");
			System.out.println((TreeMap<String, Map>) ObjSerialize.Deserialize("allDomainReport"));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetReport_002() {
		Logger.start(true, "SYSINFO日志摘取序列化本地持久化");
		try {
			//{domain={[time={LoadAverage=,...}]}
			TreeMap<String, Map> allSysInfo = new TreeMap<String, Map>();
			this.getYmtDomain().forEach(domain -> {
				String start = "2018-11-15 23:55";
				Integer offset = 60 * 16;
				Date sdate = YMTDateUtil.getDate(start, "yyyy-MM-dd HH:mm");
				Date edate = YMTDateUtil.getBeforeOrNextSecond(sdate, offset);
				ArrayList<GroupChart> listReportData = new ArrayList<GroupChart>();
				GetDataCall GetDataCall = new GetDataCall();
				getreportBean.setAppId(domain);
				getreportBean.setStartTime(YMTDateUtil.getFormatDateString(sdate, "yyyy-MM-dd HH:mm"));
				getreportBean.setEndTime(YMTDateUtil.getFormatDateString(edate, "yyyy-MM-dd HH:mm"));
				getreportBean.setChartType("SYSINFO");
				getreportBean.setTimeSpan("1");
				getreportBean.setUnit("minutes");
				GetDataCall.setData(getreportBean);
				try {
					GetDataCall.callService();
					listReportData = GetDataCall.getResult();
				} catch (Exception e) {
					e.printStackTrace();
				}
				sdate = edate;
				edate = YMTDateUtil.getBeforeOrNextSecond(sdate, offset);
				List<String> getInfoLabel = Arrays.asList("LoadAverage", "ParNewTime", "ParNewCount", "ActiveThread","ConcurrentMarkSweepTime","ConcurrentMarkSweepCount");
				TreeMap<String, Map> sysInfo = new TreeMap<String, Map>();
				// 初始化sysInfo
				listReportData.stream().forEach(rd -> {
					rd.getChartList().get(0).getxAxisData().stream().forEach(ad -> {
						sysInfo.put(ad, new HashMap<>());
					});
				});
				listReportData.stream().forEach(rd -> {
					rd.getChartList().forEach(chart -> {
						System.out.println(chart.getName());
						if (getInfoLabel.contains(chart.getName())) {
							List<String> listad = chart.getxAxisData().stream().collect(Collectors.toList());
							List<Number> listdata = chart.getSeries().get(0).getData();
							if (listad.size() == listdata.size()) {
								for (int i = 0; i < listad.size(); i++) {
									Map m = sysInfo.get(listad.get(i));
									m.put(chart.getName(), listdata.get(i).doubleValue());
								}
							}
						}
					});
				});
				allSysInfo.put(getreportBean.getAppId(), sysInfo);
			});
			ObjSerialize.Serialize(allSysInfo, "allSysInfo");
			System.out.println((TreeMap<String, Map>) ObjSerialize.Deserialize("allSysInfo"));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetReport_003() {
		Logger.start(true, "SYSINFO日志反序列化分析");
		try {
			//{domain={[time={LoadAverage=,...}]}
			TreeMap<String, Map> allSysInfo = new TreeMap<String, Map>();
			allSysInfo=(TreeMap<String, Map>) ObjSerialize.Deserialize("allSysInfo20181116");
			List<String> domainlist = this.getYmtDomain();
			
			for (String domain : domainlist) {
				//开始时间
				Integer starttime=152355;
				//持续时间
				Integer ltime=15;
				//数据
				Map domainAllInfo=allSysInfo.get(domain);
				for(int i=0;i<ltime;i++){
					 Map domainTimelineInfo = (Map) domainAllInfo.get(starttime.toString());
					 if(domainTimelineInfo!=null){
						 
						 if(Double.valueOf(domainTimelineInfo.get("ParNewTime").toString())>20||Double.valueOf(domainTimelineInfo.get("ConcurrentMarkSweepTime").toString())>20){
							 Logger.debug(domain+":"+starttime+":"+domainTimelineInfo);
						 }
					 }
					starttime++;
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}